-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple constant folding for bounds upper offsets #1095
Conversation
clang/lib/Sema/SemaBounds.cpp
Outdated
} | ||
llvm::APSInt ElemSize; | ||
if (!BoundsUtil::getReferentSizeInChars(S.Context, Base->getType(), ElemSize)) | ||
return false;; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Extra semicolon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
This PR introduces a temporary solution for an issue with bounds validation: the bounds checker should be able to prove that
bounds(p, p + ((len + 1) - 1))
implybounds(p, p + len)
.The solution involves performing constant folding for a limited set of expressions. If the upper offset of a range is of the form
(e +/- a) +/ b
, wherea
andb
are integer constants ande
is an expression, we extract the variable part ase
and the constant part asa + b
(ora + -b
,-a + b
, or-a + -b
).This is intended to be a temporary fix until we can solve issues with constant folding, commutativity, and associativity in bounds expressions in a more general way.
This fix addresses +1/-1 situations that can arise from passing arguments to functions with declared parameter bounds (see checkedc/450). However, it does not fix +1/-1 situations that can occur as a result of bounds widening, e.g.
The bounds checker is unable to create a base range for the inferred bounds
bounds(p, (p + (len - 1)) + 1)
. We need further work to address associativity issues with bounds expressions.